package com.edu.base.ums.bannerConfig.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.edu.base.ums.bannerConfig.domain.BannerPosition;
import com.edu.base.ums.bannerConfig.domain.TbBannerConfig;
import com.edu.base.ums.bannerConfig.mapper.TbBannerPositionMapper;
import com.edu.base.ums.bannerConfig.service.ITbBannerConfigRyService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * banner配置Controller
 * 
 * @author lzs
 * @date 2021-02-23
 */
@RestController
@RequestMapping("/operation/bannerConfig")
public class TbBannerConfigRyController extends BaseController
{
    @Autowired
    private ITbBannerConfigRyService tbBannerConfigRyService;
    
    @Autowired
    private TbBannerPositionMapper  tbBannerPositionMapper;

    /**
     * 查询banner配置列表
     */
    @PreAuthorize("@ss.hasPermi('operation:bannerConfig:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbBannerConfig tbBannerConfigRy)
    {
        startPage();
        List<TbBannerConfig> list = tbBannerConfigRyService.selectTbBannerConfigRyList(tbBannerConfigRy);
        return getDataTable(list);
    }

    /**
     * 导出banner配置列表
     */
    @PreAuthorize("@ss.hasPermi('operation:bannerConfig:export')")
    @Log(title = "banner配置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TbBannerConfig tbBannerConfigRy)
    {
        List<TbBannerConfig> list = tbBannerConfigRyService.selectTbBannerConfigRyList(tbBannerConfigRy);
        ExcelUtil<TbBannerConfig> util = new ExcelUtil<TbBannerConfig>(TbBannerConfig.class);
        return util.exportExcel(list, "bannerConfig");
    }

    /**
     * 获取banner配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('operation:bannerConfig:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(tbBannerConfigRyService.selectTbBannerConfigRyById(id));
    }

    /**
     * 新增banner配置
     */
    @PreAuthorize("@ss.hasPermi('operation:bannerConfig:add')")
    @Log(title = "banner配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbBannerConfig tbBannerConfigRy)
    {
        return toAjax(tbBannerConfigRyService.insertTbBannerConfigRy(tbBannerConfigRy));
    }

    /**
     * 修改banner配置
     */
    @PreAuthorize("@ss.hasPermi('operation:bannerConfig:edit')")
    @Log(title = "banner配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbBannerConfig tbBannerConfigRy)
    {
        return toAjax(tbBannerConfigRyService.updateTbBannerConfigRy(tbBannerConfigRy));
    }

    /**
     * 删除banner配置
     */
    @PreAuthorize("@ss.hasPermi('operation:bannerConfig:remove')")
    @Log(title = "banner配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(tbBannerConfigRyService.deleteTbBannerConfigRyByIds(ids));
    }
    
   
	@PreAuthorize("@ss.hasPermi('operation:bannerConfig:query')")
    @GetMapping(value = "/findBannerPositionList")
	public AjaxResult findBannerPositionList(){
        BannerPosition bannerPosition = new BannerPosition(); 
        List<BannerPosition> bannerPositionList = tbBannerPositionMapper.findList(bannerPosition);
		return AjaxResult.success(bannerPositionList);
	}
	
	 /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('operation:bannerConfig:edit')")
    @Log(title = "banner配置", businessType = BusinessType.UPDATE)
    @PostMapping("/handleUpdateStatus")
    public AjaxResult handleUpdateStatus(String[] ids,String publishStatus)
    {
    	boolean flag =tbBannerConfigRyService.handleUpdateStatus(ids,publishStatus);
    	if(!flag) {
    		return  AjaxResult.error("广告的开始时间小于现在的时间，不能上线，请重新设置开始时间和结束时间");
    	}
        return AjaxResult.success();
    }
}
